﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraTab;
using ProgNetDxComponents.ExtenderProviders.Language;

namespace ProgNetDxComponents.Controls.Editors
{
    //public partial class MultiEditViewControl : UserControl
    public partial class MultiEditViewControl : MultiEditBaseTab
    {
        public MultiEditViewControl()
        {
            InitializeComponent();
            gcMain.DataSource = DataView;
        }
        public override DataTable DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;
                gvMain.Columns.Clear();
                int idx = 0;
                if (gvMain.Columns.Count == 0)
                {
                    if (value != null)
                    {
                        foreach (DataColumn col in value.Columns)
                        {
                            GridColumn c = new GridColumn();
                            c.FieldName = col.ColumnName;
                            if (this.IsAppRunning())
                            {
                                string caption = ProgNetLanguage.sGetString(col.ColumnName);
                                if (caption != ProgNetLanguage.NoEntryString)
                                    c.Caption = caption;
                            }
                            c.Visible = true;
                            c.VisibleIndex = idx;
                            idx++;
                            gvMain.Columns.Add(c);
                        }
                    }
                }
            }
        }

        public GridColumnCollection Columns
        {
            get { return gvMain.Columns; }
        }

        private void gvMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OnRowSelected();
            }
        }

        private void gvMain_DoubleClick(object sender, EventArgs e)
        {
            OnRowSelected();
        }

        public override DataRow GetSelectedRow()
        {
            return gvMain.GetFocusedDataRow();
        }

        public override void FocusValue(object editValue)
        {
            if (editValue != null)
            {
                DataRow r = DataSource.SelectFirstRow(ValueMember + " = " + editValue.AsString().EscapeSql());
                if (r != null)
                {
                    int sourceIdx = DataSource.Rows.IndexOf(r);
                    int idx = gvMain.GetRowHandle(sourceIdx);
                    if (gvMain.IsValidRowHandle(idx))
                        gvMain.FocusedRowHandle = idx;
                }
                else
                {
                    if (gvMain.RowCount > 0)
                        gvMain.FocusedRowHandle = 0;
                }
            }
            else
            {
                if (gvMain.RowCount > 0)
                    gvMain.FocusedRowHandle = 0;
            }
        }
    }

    public class RowSelectEventArgs : EventArgs
    {
        private DataRow _Row;
        public RowSelectEventArgs(DataRow row)
        {
            _Row = row;
        }

        public DataRow Row
        {
            get { return _Row; }
        }
    }
}
